﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using PagedList;
using PagedList.Mvc;

namespace Project_2._2.Controllers.UserControllers
{
    public class TimKiemController : Controller
    {
        //
        // GET: /TimKiem/
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        public ActionResult SearchNormal(int? page, FormCollection col)
        {
            try
            {
                string key_search = col["SearchString"].ToString();

                //ViewBag.id = id;

                List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

                var cars = (from xe in db.Xes
                            join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                            join gia in db.GiaXes on xe.XeId equals gia.XeId
                            join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                            join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                            where xe.TenXe.Contains(key_search) || xe.DongXe.Contains(key_search) || th.TenThuongHieu.Contains(key_search) && xe.Duyet == true && xe.isDelete == false
                            select new
                            {
                                id = xe.XeId,
                                name = xe.TenXe,
                                THieu = th.TenThuongHieu,
                                Doixe = xe.DoiXe,
                                DongXe = xe.DongXe,
                                Color = xe.MauXe,
                                Gia = gia.Gia,
                                Loai = loai.TenLoaiXe,
                                Hinh = xe.HinhAnhDaiDien,
                                TThai = xe.TrangThai,
                                dvid = dv.DichVuId,
                            }).ToList();

                CarList_ViewModel carslist;
                if (cars.Count > 0)
                {
                    foreach (var item in cars)
                    {
                        carslist = new CarList_ViewModel();
                        carslist.id = item.id;
                        carslist.name = item.name;
                        carslist.THieu = item.THieu;
                        carslist.DoiXe = item.Doixe;
                        carslist.DongXe = item.DongXe;
                        carslist.Color = item.Color;
                        carslist.Loai = item.Loai;
                        carslist.TThai = item.TThai;
                        carslist.Gia = item.Gia;
                        carslist.hinh = item.Hinh;
                        carslist.dichvuID = item.dvid;
                        listcar.Add(carslist);
                    }
                    var carReturn = listcar.ToPagedList(page ?? 1, 8);
                    return View(carReturn);
                }
                return View();
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }           
        }

        public ActionResult SearchAdvance(int? page, FormCollection col)
        {
            string key_TenXe = col["Search_TenXe"].ToString();
            int key_HieuXe = int.Parse(col["Search_HieuXe"]);
            int key_LoaiXe = int.Parse(col["Search_LoaiXe"]);
            decimal key_PriceFrom = decimal.Parse(col["Search_PriceFrom"]);
            decimal key_PriceTo = decimal.Parse(col["Search_PriceTo"]);

            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        where xe.TenXe.Contains(key_TenXe) &&
                              th.ThuongHieuId == key_HieuXe || key_HieuXe == 0 &&
                              loai.LoaiXeId == key_LoaiXe || key_LoaiXe == 0 &&
                              key_PriceFrom <= gia.Gia &&
                              key_PriceTo >= gia.Gia && 
                              xe.Duyet == true && xe.isDelete == false
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            DongXe = xe.DongXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Gia = gia.Gia,
                            Hinh = xe.HinhAnhDaiDien,
                            TThai = xe.TrangThai,
                            dvid = dv.DichVuId,
                        }).ToList();

            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.DongXe = item.DongXe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.TThai = item.TThai;
                    carslist.Gia = item.Gia;
                    carslist.hinh = item.Hinh;
                    carslist.dichvuID = item.dvid;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 8);
                return View(carReturn);
            }
            return View();
        }



    }
}
